// http 拦截器
import axios from "axios";
import qs from "qs";
import {clearAllStore, getStore} from "/src/api/utils.js";
import {LoadingBar, Message} from "view-ui-plus";
import router from "@/router";


// api方法白名单
let whiteApi = [
    '/api/admin/login'
]

const http = axios.create({
    timeout: 5000
});

LoadingBar.config({
    color: '#5cb85c',
    failedColor: '#f0ad4e',
    height: 5
})

http.interceptors.request.use(
    config => {
        if (whiteApi.indexOf(config.url) == -1) {
            config.headers['token'] = getStore('token') ?? ''
        }
        let param = {...config.data}
        config.method = config.method || 'GET'
        if (config.method == 'post') {
            config.data = qs.stringify(param)
            config.params = ''
        } else {
            config.params = param
        }
        config.method.toUpperCase()
        // console.log(config)
        LoadingBar.start()
        return config
    },
    err => {
        console.log(err)
        LoadingBar.error()
        // app.config.globalProperties.$loading.hideLoading();
        return Promise.reject(err)
    }
)

http.interceptors.response.use(
    response => {
        switch (response.status) {
            case 200:
                LoadingBar.finish()
                return response.data
                break;
            case 202:
                Message.error({
                    content: response.data.data.data,
                    duration: 1.5,
                    closeable: true
                })
                LoadingBar.error()
                clearAllStore()
                router.push('/login')
                break;
        }
        console.log(response)
        // if (response.status === 200) {
        //
        // }
        // if (response.status === 202) {
        //     Message.error({
        //         content: response.data.msg,
        //         duration: 1.5,
        //         closeable: true
        //     })
        //     LoadingBar.error()
        //     clearAllStore()
        //     router.push('/login')
        // }
        // if (response.status === 403) {
        //     Message.error({
        //         content: response.data.data,
        //         duration: 1.5,
        //         closeable: true
        //     })
        //     LoadingBar.error()
        // }
    },
    error => {
        switch (error.response.status) {
            case 202:
                Message.error({
                    content: error.response.data.data.data,
                    duration: 1.5,
                    closeable: true
                })
                LoadingBar.error()
                clearAllStore()
                router.push('/login')
                return Promise.reject(error.response.data.data)
                break;
            case 403:
                Message.error({
                    content: error.response.data.data,
                    duration: 1.5,
                    closeable: true
                })
                LoadingBar.error()
                console.log(error.response)
                return Promise.reject(error.response.data.data)
                break;
            // return error.response
            default:
                Message.error({
                    content: '系统错误,请联系管理员!',
                    duration: 1.5,
                    closeable: true
                })
                LoadingBar.error()
                return Promise.reject(error.response.data.data)
        }
        return error.response
    }
)

export default http;
